#!/bin/sh
#
# Copyright (c) 1997 Silicon Graphics, Inc.  All Rights Reserved.
# Copyright (c) 2017 Ken McDonell.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# Create pmdbg.h from ../../include/pcp/.h
#
# Recipe for adding a new debug flag (newie):
# 1. edit ../../include/pcp/pmapi.h
#    - Add the new entry into pmdebugoptions_t ... the name must obviously
#      be unique, and the comment on the same line becomes the explanation
#      that is visible with pmdbg -l, so be careful with the wordsmithing.
#      The order of the "old" options is important, so add the new entry
#      after the /* new ones start here ... */ comment, probably at the end
#      of the declaration block for pmdebugoptions_t.
# 2. $ make
#    - Will recreate ./pmdbg.h and rebuild libpcp
#
# For testing:
# 3. $ sudo make install
# 4. $ cd ../../pmdbg
#    $ make clean
#    $ make
#    $ ./pmdbg -l
#    - Check "newie" flag appears and description is OK
#    $ sudo make install
# 5. $ cd ../dbpmda/src
#    $ make clean
#    $ make
#    - run these commands to make sure the new flags are listed:
#    $ ./dbpmda
#    dbpmda> debug appl0 newie
#    dbpmda> status
#    - newie should be listed on the Debug options: line
#    dbpmda> debug -newie
#    dbpmda> status
#    - newie should NOT be listed on the Debug options: line
#    $ sudo make install
# 6. $ cd ../../../qa
#    $ export PCPQA_VALGRIND=both
#    $ ./remake 1277 1278 1359
#    $ ./check -g pmdbg
#    - and remake/fix any other QA failures
#

INCDIR=../../include

if [ -f $INCDIR/pcp.conf ]
then
    # needed for $PCP_SORT_PROG
    . $INCDIR/pcp.conf
else
    echo "mk_pmdbg: cannot find pcp.conf"
    exit 1
fi

for file in pmapi.h
do
    if [ ! -f $INCDIR/pcp/$file ]
    then
	echo "mk_pmdbg: cannot find $file"
	exit 1
    fi
done

sedcmd=`$PCP_AWK_PROG <$INCDIR/pcp/pmapi.h '
BEGIN			{ start=0 }
/^typedef struct [{]/	{ start=NR }
/^} pmdebugoptions_t;/	{ if (start>0) print start "," NR "p"; exit }'`
if [ -z $sedcmd ]
then
    echo "mk_pmdbg: cannot find __pmdebugoptions typedef in pmapi.h"
    exit 1
fi

if [ -n "$PCP_TMPFILE_DIR" -a -d "$PCP_TMPFILE_DIR" ]
then
    tmp=`mktemp -d "$PCP_TMPFILE_DIR/pcp-build-mk.pmdbg.XXXXXXXXX"` || exit 1
else
    # assume run during a build and /tmp is a safe bet
    #
    tmp=`mktemp -d "/tmp/pcp-build-mk.pmdbg.XXXXXXXXX"` || exit 1
fi
trap "rm -rf $tmp; exit 0" 0 1 2 3 15

rm -f pmdbg.h
cat <<End-of-File >pmdbg.h
/*
 * DO NOT EDIT.
 * Built from ../../include/pcp/{pmapi,deprecated}.h by mk_pmdbg.
 * Any modifications here will be lost.
 */

typedef struct {
    const char	*name;
    int	* const options;	/* points to a pmDebugOptions.xxx */
    int		state;		/* for internal use */
    const char	*text;		/* explanation */
} debug_map_t;

static debug_map_t debug_map[] = {
End-of-File

# debug options look like this ...
#	int	exec;	 	/* pmExec tracing */
#
sed -n -e $sedcmd <$INCDIR/pcp/pmapi.h \
| sed -n \
    -e '/[ 	]*int[ 	][ 	]*\([a-z][a-z0-9_]*\);/{
s//\1/
s/[ 	].*\/\* */ /
s/ *\*\///
p
}' \
| while read option text
do
    echo "    { \"$option\", &pmDebugOptions.$option, 0, \"$text\" }," >>pmdbg.h
done

cat <<End-of-File >>pmdbg.h
};

static const int num_debug = sizeof(debug_map) / sizeof(debug_map[0]);
End-of-File

exit
